//
//  HeroInfo.m
//  FirstMatchThree
//
//  Created by Petar on 5/23/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "HeroInfo.h"
#import "Globals.h"

@implementation HeroInfo

- (id)initWithCoder:(NSCoder *)decoder
{
    
    if ( (self = [super init]) ) {
        
        //decode properties, other class vars
        self.isOwned = [decoder decodeBoolForKey:@"isOwned"];
        self.isChosen = [decoder decodeBoolForKey:@"isChosen"];
        self.produces = [decoder decodeIntegerForKey:@"produces"];
        self.attack = [decoder decodeIntegerForKey:@"attack"];
        self.type = [decoder decodeIntegerForKey:@"type"];
        self.price = [decoder decodeIntegerForKey:@"price"];
        self.name = [decoder decodeObjectForKey:@"name"];
        
    }
    
    return self;
    
}


- (void)encodeWithCoder:(NSCoder *)encoder
{
    
    //Encode properties, other class variables, etc
    [encoder encodeBool:self.isOwned forKey:@"isOwned"];
    [encoder encodeBool:self.isChosen forKey:@"isChosen"];
    [encoder encodeInteger:self.produces forKey:@"produces"];
    [encoder encodeInteger:self.attack forKey:@"attack"];
    [encoder encodeInteger:self.type forKey:@"type"];
    [encoder encodeInteger:self.price forKey:@"price"];
    [encoder encodeObject:self.name forKey:@"name"];
    
}


+ (NSArray *)getLatestHeroInfoArray
{
    
    NSArray *heroeNames = @[ @"Sam",           // 1
                             @"Lu",            // 2
                             @"Popy",          // 3
                             @"Flash",         // 4
                             @"Funny Bunny",   // 5
                             @"Captain Sword", // 6
                             @"Kevin",         // 7
                             @"Shee",          // 8
                             @"Black Tom",     // 9
                             @"Super F",       // 10
                             @"Dream",         // 11
                             @"Sponge Bolt",   // 12
                             @"Sugar Pig",     // 13
                             @"Skippy",        // 14
                             @"Jolly"          // 15
                             ];
    
    NSMutableArray *latestHeroInfoArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < [heroeNames count]; i++) {
        
        HeroInfo *heroInfo = [[HeroInfo alloc] init];
        heroInfo.name = heroeNames[i];
        heroInfo.isOwned = i == 0 || i == 1 ? YES : NO; // Makes 1 and 2 heroes to be Unlocked
        heroInfo.isChosen = i == 0 ? YES : NO; // Makes firstHero to be chosen
        heroInfo.produces = 2;
        heroInfo.attack = -1;
        heroInfo.type = i + 1; // from 1 to [heroeNames count]
        heroInfo.price = 200; // initial price
        
        [latestHeroInfoArray addObject:heroInfo];
        
    }
    
    return latestHeroInfoArray;
    
}


// Saves NSArray of HeroInfo objects in NSUserDefaults
+ (void)saveHeroInfoArray:(NSArray *)heroInfoArray
{
    
    // Encodes the array with HeroInfo objects
    NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:heroInfoArray];
    [[NSUserDefaults standardUserDefaults] setObject:myEncodedObject forKey:HERO_INFO_ARRAY_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}


+ (HeroInfo *)getChosenHeroInfo
{
    
    NSArray *heroInfoArray = [HeroInfo getHeroInfoArray];
    
    for (HeroInfo *heroInfo in heroInfoArray) {
        
        if ( heroInfo.isChosen == YES ) {

            return heroInfo;
            
        }
        
    }
    
    // There is No chosen Hero
    return nil;
    
}


// Get NSArray of HeroInfo objects from NSUserDefaults
+ (NSArray *)getHeroInfoArray
{
    
    // Decodes the array with HeroInfo objects
    NSData *myDecodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:HERO_INFO_ARRAY_KEY];
    
    NSArray *heroInfoArray = [NSArray new];
    
    if ( myDecodedObject != nil ) {
        heroInfoArray = [NSKeyedUnarchiver unarchiveObjectWithData:myDecodedObject];
    }
    
    return heroInfoArray;
    
}


+ (void)updateHeroInfoModel
{
    
    NSArray *currentHeroInfoArray = [NSArray arrayWithArray:[HeroInfo getHeroInfoArray]];
    NSArray *latestHeroInfoArray = [HeroInfo getLatestHeroInfoArray];
    
    // No need to Update heroInfoModel if the arrays are the same
    if ( [currentHeroInfoArray count] >= [latestHeroInfoArray count] ) {
        return;
    }
    
    NSMutableArray *newHeroInfoObjects = [NSMutableArray new];
    
    // Find the new HeroInfo objects
    for (int i = 0; i < [latestHeroInfoArray count]; i++) {
        
        BOOL hasToAddHeroInfo = YES;
        HeroInfo *latestHeroInfo = latestHeroInfoArray[i];
        
        for ( int k = 0 ; k < [currentHeroInfoArray count]; k++) {
            
            HeroInfo *currentHeroInfo = currentHeroInfoArray[k];
            
            // If the types are equal, I have already have it
            if ( latestHeroInfo.type == currentHeroInfo.type ) {
                hasToAddHeroInfo = NO;
                break;
            }
            
        }
        
        // Adds only heroInfo objects, which types I don't have
        if ( hasToAddHeroInfo == YES ) {
            [newHeroInfoObjects addObject:latestHeroInfo];
        }
        
    }
    
    NSMutableArray *updatedHeroInfoArray = [NSMutableArray arrayWithArray:currentHeroInfoArray];
    [updatedHeroInfoArray addObjectsFromArray:newHeroInfoObjects];
    
    [HeroInfo saveHeroInfoArray:updatedHeroInfoArray];
    
}


- (NSString *)description
{
    
    return [NSString stringWithFormat:@"%@ isOwned: %@, isChosen: %@, produces: %zd, attack: %zd, type: %zd, price: %zd",
            [super description],
            self.isOwned ? @"YES" : @"NO",
            self.isChosen ? @"YES" : @"NO",
            self.produces,
            self.attack,
            self.type,
            self.price
            ];
    
}


@end
